#!/bin/sh
#
# This file is a part of the open-eBackup project.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at
# http://mozilla.org/MPL/2.0/.
#
# Copyright (c) [2024] Huawei Technologies Co.,Ltd.
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
set +x
#@function: check DB status, Four capacities need to be returned.
# 0 check if close, 1 check if open, 2 check if exists

SCRIPT_ROOT_PATH=$1
PID=$2
PARAM_NUM=$3

. "${SCRIPT_ROOT_PATH}"/oracle_sbin_func.sh

#********************************define these for the functions of agent_sbin_func.sh********************************

# global var, for kill monitor
PARAM_CONTENT=`read_input_param "${PARAM_NUM}"`
test -z "$PARAM_CONTENT"                                        && exit_with_error "PARAM_FILE"

DBNAME=`get_value "${PARAM_CONTENT}" AppName`
DBINSTANCE=`get_value "${PARAM_CONTENT}" InstanceName`
DBUSER=`get_value "${PARAM_CONTENT}" UserName`
DBUSERPWD=`get_value "${PARAM_CONTENT}" Password`
# 0 check if close, 1 check if open, 2 check if exists
CMDTYPE=`get_value "${PARAM_CONTENT}" CheckType`
ORA_DB_USER=`get_value "${PARAM_CONTENT}" OracleInstallUser`
ORACLE_BASE=`get_value "${PARAM_CONTENT}" AccessOracleBase`
ORACLE_HOME=`get_value "${PARAM_CONTENT}" AccessOracleHome`

test "$DBNAME" = "ERROR_PARAM_INVALID"                       && exit_with_error "DBNAME"
test "$DBINSTANCE" = "ERROR_PARAM_INVALID"                   && exit_with_error "DBINSTANCE"
test "$DBUSER" = "ERROR_PARAM_INVALID"                       && exit_with_error "DBUSER"
test "$CMDTYPE" = "ERROR_PARAM_INVALID"                      && exit_with_error "CMDTYPE"

log_info "PID=${PID};DBINSTANCE=${DBINSTANCE};DBName=${DBNAME};DBUSER=${DBUSER};\
CMDTYPE=$CMDTYPE;ORACLE_HOME=$ORACLE_HOME;ORACLE_BASE=$ORACLE_BASE"

test -z "${CMDTYPE}" && exit_with_error "CmdType is NULL"
test -z "${DBINSTANCE}" && exit_with_error "DBINSTANCE is NULL"

delete_file "${RESULT_FILE}"

#********************************check host enviroment begin********************************
#get oracle user info
get_oracle_user
get_oracle_user_shell_type
env_file_list=`get_user_env_file "${ORA_DB_USER}"`
if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
    log_error "Get user env file failed"
    exit "$RETURN_INVALID"
fi

EXPORT_ORACLE_ENV=`generate_export_cmd "$env_file_list" "oracle"`
if [ $? -ne 0 ] || [ -z "$EXPORT_ORACLE_ENV" ]; then
    log_error "Get user env cmd failed"
    exit "$RETURN_INVALID"
fi

# check oracle install
check_oracle_is_install

# check oracle install type
check_oracle_install_type



if [ "$DBISCLUSTER" -gt 0 ]; then
    # get grid user
    get_grid_user
    get_grid_user_shell_type
    get_grid_home_path

    env_file_list=`get_user_env_file "${ORA_GRID_USER}"`
    if [ $? -ne 0 ] || [ -z "$env_file_list" ]; then
        log_error "Get user env file failed"
        exit "$RETURN_INVALID"
    fi

    EXPORT_GRID_ENV=`generate_export_cmd "$env_file_list" "grid"`
    if [ $? -ne 0 ] || [ -z "$EXPORT_GRID_ENV" ]; then
        log_error "Get user env cmd failed"
        exit "$RETURN_INVALID"
    fi
fi

get_oracle_base_path

get_oracle_home_path

get_oracle_version

check_sqlplus_status

check_rman_status

DBINSTANCE=`get_real_instance_name "${DBINSTANCE}"`
log_info "Check cluster [DBISCLUSTER=${DBISCLUSTER}], DBINSTANCE=${DBINSTANCE}."


get_instance_process_status "${DBINSTANCE}"
instance_stataus=$?
if [ "${CMDTYPE}" = "0" ] && [ $instance_stataus = "$INSTANCE_OFFLINE" ]; then
    exit 0
elif [ "${CMDTYPE}" = "1" ] && [ $instance_stataus = "$INSTANCE_ONLINE" ]; then
    exit 0
elif [ "${CMDTYPE}" = "2" ]; then
    grep "^${DBINSTANCE}:" /etc/oratab
    if [ $? -eq 0 ]; then
        log_info "instance ${DBINSTANCE} is already exists."
        exit 0
    fi
elif [ "${CMDTYPE}" = "3" ]; then
    log_info "Start to clear database for restore, mount, instance restore, ${DBINSTANCE}."
    if [ $instance_stataus -eq "$INSTANCE_OFFLINE" ]; then
        log_info "${DBINSTANCE} is not running."
        exit 0
    fi
    
    shutdown_db "${DBINSTANCE}" "$DBUSER" "$DBUSERPWD"
    if [ $? -ne 0 ]; then
        log_error "Shutdown database failed"
        exit 1
    fi
    log_info "Shutdown database instance [${DBINSTANCE}] success."
    exit 0
fi

log_error "Check db status failed, stauts is [${instance_stataus}] ."
exit 1

